<?php

namespace App\Http\Controllers\Mini\Estate;

use App\Http\Controllers\Controller;
use App\Services\RepairsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class RepairsController extends Controller
{

    protected $service;

    public function __construct(RepairsService $repairsService)
    {
        $this->service = $repairsService;
    }


    /**
     * Notes: 添加报修
     * User: lstink
     * Date: 2021/3/4
     * Time: 3:13 下午
     * @param Request $request
     * @return array
     */
    public function store(Request $request): array
    {
        $data = $request->post();
        //数据验证
        $validator = Validator::make($data, [
            'type'    => 'required',
            'content' => 'required|min:5|max:100',
            'name'    => 'required',
            'mobile'  => 'required|size:11|starts_with:1'
        ], [
            'type.required'      => '请选择报修类型',
            'content.required'   => '请填写情况描述',
            'content.min'        => '情况描述内容最少5个字符',
            'content.max'        => '情况描述内容最多100个字符',
            'mobile.required'    => '请填写手机号',
            'mobile.size'        => '手机号长度必须是11位数字',
            'mobile.starts_with' => '手机号必须是1开头',
        ]);

        if ($validator->fails()) {
            $errors = $validator->errors();
            return $this->error($errors->first());
        }

        $info = $this->service->addRepairs($data);
        return $info['status'] ? $this->success([]) : $this->error(['msg']);
    }

    /**
     * Notes: 用户的报修列表
     * User: lstink
     * Date: 2021/3/4
     * Time: 6:28 下午
     * @return array
     */
    public function index(Request $request): array
    {
        $info = $this->service->getRepairsListByUserId($request->query());
        return $info['status'] ? $this->success($info['data']) : $this->error(['msg']);
    }

    /**
     * Notes: 取消报修
     * User: lstink
     * Date: 2021/3/5
     * Time: 10:03 上午
     * @param $id
     * @return array
     */
    public function update($id): array
    {
        $info = $this->service->cancelRepairs($id);
        return $info['status'] ? $this->success($info['data']) : $this->error($info['msg']);
    }

    /**
     * Notes: 报修详情
     * User: lstink
     * Date: 2021/3/5
     * Time: 10:57 上午
     * @param $id
     * @return array
     */
    public function show($id)
    {
        $info = $this->service->detail($id);
        return $info['status'] ? $this->success($info['data']) : $this->error($info['msg']);
    }
}
